//
//  XDCodePopView.m
//  HospitalDoctorEdition
//
//  Created by iCode on 16/7/20.
//  Copyright © 2016年 iCode. All rights reserved.
//

#import "XDCodePopView.h"

#import "Masonry.h"

@implementation XDCodePopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //初始化各种控件
        _backgroundView = [[UIView alloc] initWithFrame:frame];
        
        _backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        
        [self addSubview:_backgroundView];
        
        _contentView = [[UIView alloc] init];
        
        _contentView.backgroundColor = [UIColor whiteColor];
        
        _contentView.layer.masksToBounds = YES;
        
        _contentView.layer.cornerRadius = 5;
        
        [self addSubview:_contentView];
        
        _titleLabel = [[UILabel alloc]init];
        
        _titleLabel.text = @"输入验证码";
        
        _titleLabel.textColor = [UIColor blackColor];
        
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        
        [self.contentView addSubview:_titleLabel];
        
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_closeBtn setTitle:@"取消" forState:UIControlStateNormal];
        
        _closeBtn.backgroundColor = [UIColor whiteColor];
        
        [_closeBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        [_closeBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:_closeBtn];
        
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        
        _confirmBtn.backgroundColor = [UIColor whiteColor];
        
        [_confirmBtn setTitleColor:[UIColor colorWithRed:55.0/255.0 green:154.0/255.0 blue:12.0/255.0 alpha:1] forState:UIControlStateNormal];
        
        [self.contentView addSubview:_confirmBtn];
        
        _codeImageView = [[UIImageView alloc]init];
        
        _codeImageView.backgroundColor = [UIColor whiteColor];
        
        _codeImageView.userInteractionEnabled = YES;
        
        [self.contentView addSubview:_codeImageView];
        
        UITapGestureRecognizer *codeImageViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(codeImageViewTap)];
        
        [_codeImageView addGestureRecognizer:codeImageViewTap];
        
        _textField = [[UITextField alloc]init];
        
        _textField.layer.borderColor = [UIColor colorWithRed:200.0/255.0 green:199.0/255.0 blue:204.0/255.0 alpha:1]
.CGColor;
        
        _textField.keyboardType = UIKeyboardTypeNamePhonePad;
        
        _textField.returnKeyType = UIReturnKeyDone;
        
        _textField.layer.borderWidth = 1.0f;
                
        _textField.placeholder = @"验证码";
        
        [self.contentView addSubview:_textField];
        
        //添加布局约束
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mas_top).offset(SCREEN_HEIGHT / 4);
        make.left.equalTo(self.mas_left).offset(40);
        make.right.equalTo(self.mas_right).offset(- 40);
        make.height.mas_equalTo(SCREEN_HEIGHT / 4);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView.mas_top).offset(10);
        make.left.equalTo(self.contentView.mas_left).with.offset(20);
        make.right.equalTo(self.contentView.mas_right).with.offset( -20);
        make.height.mas_equalTo(20);
        
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_titleLabel.mas_bottom).offset((SCREEN_HEIGHT / 4 - (SCREEN_HEIGHT / 4 / 4) - 20 - 10 - SCREEN_HEIGHT / 4 / 5) / 2);
        make.left.equalTo(self.contentView.mas_left).with.offset((SCREEN_WIDTH - 80) / 6);
        make.width.mas_equalTo((SCREEN_WIDTH - 80) / 3);
        make.height.mas_equalTo(SCREEN_HEIGHT / 4 / 5);
    }];
    
    [self.codeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_titleLabel.mas_bottom).offset((SCREEN_HEIGHT / 4 - (SCREEN_HEIGHT / 4 / 4) - 20 - 10 - SCREEN_HEIGHT / 4 / 5) / 2);
        make.left.equalTo(_textField.mas_right).with.offset(20);
        make.right.equalTo(self.contentView.mas_right).with.offset( -(SCREEN_WIDTH - 80) / 6);
        make.height.mas_equalTo(SCREEN_HEIGHT / 4 / 5);
        
    }];

    
    UIView *line = [[UIView alloc]init];
    
    line.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:199.0/255.0 blue:204.0/255.0 alpha:1];
    
    [self.contentView addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(- SCREEN_HEIGHT / 4 / 4);
        make.right.equalTo(self.contentView.mas_right).offset(0);
        make.height.with.offset (1);
    }];

    UIView *line2 = [[UIView alloc]init];
    
    line2.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:199.0/255.0 blue:204.0/255.0 alpha:1];
    
    [self.contentView addSubview:line2];
    
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView.mas_left).offset((SCREEN_WIDTH - 80) / 2);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
        make.width.with.offset (1);
        make.height.with.offset (SCREEN_HEIGHT / 4 / 4);
    }];

    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView.mas_left).offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
        make.width.mas_equalTo((SCREEN_WIDTH - 80) / 2);
        make.height.mas_equalTo(SCREEN_HEIGHT / 4 / 4);
    }];
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.closeBtn.mas_right).offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
        make.right.equalTo(self.contentView.mas_right).offset(0);
        make.height.with.offset (SCREEN_HEIGHT / 4 / 4);
    }];
}

#pragma mark pop和dismiss

- (void)pop {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    
    //动画效果入场
    self.contentView.transform = CGAffineTransformMakeScale(0.2, 0.2);
    self.contentView.alpha = 0;
    [UIView animateWithDuration:.35 animations:^{
        self.contentView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        self.contentView.alpha = 1;
    }];
}

- (void)dismiss {
    //动画效果出场
    [UIView animateWithDuration:.35 animations:^{
        self.contentView.transform = CGAffineTransformMakeScale(0.2, 0.2);
        self.contentView.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            
            [self removeFromSuperview];
            
            [_textField resignFirstResponder];
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if (![touch.view isEqual:self.contentView]) {
//        [self dismiss];
        
        [_textField resignFirstResponder];
    }
}

//点击图片
- (void)codeImageViewTap
{
    [self.delegate codeImageTap];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
